import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.Timer;

public class Minimap extends JPanel {

	private Timer repaintTimer;
	private Human human;
	private ArrayList<Bugger> buggers;

	public Minimap(Human human, ArrayList<Bugger> buggers) {
		super();

		this.human = human;
		this.buggers = buggers;
		
		setBorder(BorderFactory.createLineBorder(Color.GRAY));
		setBackground(Color.BLACK);

		repaintTimer = new Timer(EndersGame.TIMER_INCREMENTS_REPAINT,
				repaintController);
		repaintTimer.start();
	}

	public void paint(Graphics g) {
		super.paint(g);

		if (!EndersGame.initial) {
			if (EndersGame.inGame) {
				if (human != null && buggers != null) {
					Graphics2D g2d = (Graphics2D) g;

					for (Bugger i : buggers) {
						// Translate position coordinates to be scaled onto the
						// Minimap
						double x = (1.0 / (double) (EndersGame.sets - 1))
								* i.getX();
						double y = (EndersGame.MINIMAP_HEIGHT / EndersGame.BOARD_HEIGHT)
								* (double) i.getY();
						g2d.drawImage(i.getMiniImage(), (int) x, (int) y, this);
					}

					// Translate position coordinates to be scaled onto the
					// Minimap
					double x = (1.0 / (double) (EndersGame.sets - 1))
							* human.getX();
					double y = (EndersGame.MINIMAP_HEIGHT / EndersGame.BOARD_HEIGHT)
							* (double) human.getY();
					g2d.drawImage(human.getMiniImage(), (int) x, (int) y, this);
					setBackground(Color.BLACK);

					if (human.incoming) {
						String msg = "Incoming";
						Font font = new Font("Helvetica", Font.BOLD, 10);
						FontMetrics metr = this.getFontMetrics(font);

						g.setColor(Color.WHITE);
						g.setFont(font);
						g.drawString(msg, 10, 10);
					}
				} else {
//					setBackground(Color.BLACK);
				}
//				setBorder(BorderFactory.createLineBorder(Color.CYAN));
			} else {
//				setBackground(Color.BLACK);
			}
		}

		Toolkit.getDefaultToolkit().sync();
		g.dispose();
	}

	public void setHuman(Human human) {
		this.human = human;
	}

	public void setBuggers(ArrayList<Bugger> buggers) {
		this.buggers = buggers;
	}

	public Human getHuman() {
		return human;
	}

	public ArrayList<Bugger> getBuggers() {
		return buggers;
	}

	// Determine which Buggers are visible and remove them as necessary, then
	// repaint.
	private ActionListener repaintController = new ActionListener() {
		public void actionPerformed(ActionEvent ae) {
			repaint();
		}
	};
}
